<template>
  <a-spin :spinning="loading">
    <div class="manage-list__box">
      <a-page-header
        style="border: 1px solid rgb(235, 237, 240)"
        title="菜单列表"
      >
        <template slot="extra">
          <a-button key="addNew" type="primary" @click="()=>changeModal(true)">
            新增菜单
          </a-button>
        </template>
        </a-page-header>
      <a-table bordered :columns="columns" :data-source="data" size="middle">
        <template
            v-for="col in ['name', 'icon', 'path']"
            :slot="col"
            slot-scope="text, record"
        >
          <div :key="col">
            <a-input
                v-if="record.editable"
                style="margin: -5px 0"
                :value="text"
                @change="e => handleChange(e.target.value, record.id, col)"
            />
            <template v-else>
              {{ text }}
            </template>
          </div>
        </template>
        <template slot="higher_text" slot-scope="text, record">
          <div key="higher_text">
            <a-select v-if="record.editable" style="margin: -5px 0;width: 100%" :value="text" @change="e => handleChange(e, record.id, 'higher_text')">
              <a-select-option value="0~无上级菜单" v-bind:key="0">
                无上级菜单
              </a-select-option>
              <template v-for="item in data">
                <a-select-option :value="item.id+'~'+item.name" v-bind:key="item.id" v-if="item.id!==record.id">
                  {{ item.name }}
                </a-select-option>
              </template>
            </a-select>
            <template v-else>
              {{ text }}
            </template>
          </div>
        </template>
        <template slot="action" slot-scope="text, record">
          <div class="editable-row-operations">
            <span v-if="record.editable">
              <a @click="() => save(record.id)">保存</a>
              <a-divider type="vertical" />
              <a @click="() => cancel(record.id)">取消</a>
            </span>
            <span v-else>
              <a :disabled="editingKey !== ''" @click="() => edit(record.id)">编辑</a>
              <a-divider type="vertical" />
              <a-popconfirm :title="`确认删除${record.name}吗？`" @confirm="() => deleteMenu(record)">
                <a>删除</a>
              </a-popconfirm>
            </span>
          </div>
        </template>
      </a-table>
      <AddMenu  :modelVisible="modelVisible" :submit="addMenuAction" :handleClose="()=>changeModal(false)" :selectData="data"></AddMenu>
    </div>
  </a-spin>
</template>

<script>
import { mapState, mapActions, mapMutations } from 'vuex'
import {
  GET_LIST_ACTION,
  SET_LIST_MUTATION,
  SET_CACHE_LIST_MUTATION,
  SET_EDITING_KEY_MUTATION,
  SET_LOADING_MUTATION,
  ADD_MENU_ACTION,
  DELETE_MENU_ACTION,
  SET_MODEL_MUTATION,
    UPDATE_MENU_ACTION
} from './constants'
import AddMenu from "../../components/addMenu";
export default {
  name: "Menu",
  data() {
    return {
      columns:[
        { title: '菜单名称', align: 'center', width: '20%', dataIndex: 'name', key: 'name', scopedSlots: { customRender: 'name' } },
        { title: '上级菜单', align: 'center', width: '20%', dataIndex: 'higher_text', key: 'higher', scopedSlots: { customRender: 'higher_text' } },
        { title: '图标', align: 'center', width: '20%', dataIndex: 'icon', key: 'icon', scopedSlots: { customRender: 'icon' } },
        { title: '路径', align: 'center', width: '20%', dataIndex: 'path', key: 'path', scopedSlots: { customRender: 'path' } },
        { title: '操作', align: 'center', width: '20%', dataIndex: 'action', key: 'action', scopedSlots: { customRender: 'action' } },
      ],
    }
  },
  /**
   * computed会把vuex store里面的值拿到当前页面上来
   */
  computed: mapState({
    loading: state => state.Menu.loading,
    data: state => state.Menu.data,
    cacheData: state => state.Menu.cacheData,
    editingKey: state => state.Menu.editingKey,
    modelVisible: state => state.Menu.modelVisible,
  }),
  components: { AddMenu },
  created() {
    this.getListAction();
  },
  methods: {
    /**
     * mapMutations把vuex Mutations里面的方法拿到当前页面来 这是用来更新store里面的值 同步执行
     */
    ...mapMutations({
      setData:`Menu/${SET_LIST_MUTATION}`,
      setCacheData:`Menu/${SET_CACHE_LIST_MUTATION}`,
      setEditingKey:`Menu/${SET_EDITING_KEY_MUTATION}`,
      setLoading:`Menu/${SET_LOADING_MUTATION}`,
      setModal:`Menu/${SET_MODEL_MUTATION}`,
    }),
    /**
     * mapActions把vuex actions里面的方法拿到当前页面来 这是用来处理异步以及复杂业务 通过await实现同步执行
     */
    ...mapActions({
      getListAction:`Menu/${GET_LIST_ACTION}`,
      addMenuAction:`Menu/${ADD_MENU_ACTION}`,
      deleteMenuAction:`Menu/${DELETE_MENU_ACTION}`,
      updateMenuAction:`Menu/${UPDATE_MENU_ACTION}`,
    }),
    changeModal(show){
      this.setModal(show)
    },
    deleteMenu(obj){
      this.setLoading(true);
      this.deleteMenuAction(obj.id);
      this.setLoading(false);
    },
    handleChange(value, key, column) {
      const newData = [...this.data];
      const target = newData.filter(item => key === item.id)[0];
      if (column==='higher_text'){
        const val = value.split('~');
        if (target) {
          target[column] = val[1];
          target['higher'] = val[0];
          this.setData(newData);
        }
      }else {
        if (target) {
          target[column] = value;
          this.setData(newData);
        }
      }



    },
    edit(key) {
      const newData = [...this.data];
      const target = newData.filter(item => key === item.id)[0];
      this.setEditingKey(key);
      if (target) {
        target.editable = true;
        this.setData(newData);
      }
    },
    save(key) {
      const newData = [...this.data];
      const newCacheData = [...this.cacheData];
      const target = newData.filter(item => key === item.id)[0];
      const targetCache = newCacheData.filter(item => key === item.id)[0];
      if (target && targetCache) {
        delete target.editable;
        this.setData(newData);
        Object.assign(targetCache, target);
        this.setCacheData(newCacheData);
        this.updateMenuAction(target);
      }
      this.setEditingKey('');
    },
    cancel(key) {
      const newData = [...this.data];
      const target = newData.filter(item => key === item.id)[0];
      this.setEditingKey('');
      if (target) {
        Object.assign(target, this.cacheData.filter(item => key === item.id)[0]);
        delete target.editable;
        this.setData(newData);
      }
    },
  },
}
</script>

<style lang="less" scoped>
.manage-list__box{
  padding: 20px;
}
</style>
